/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.open.api.core.service.processor.base.post;

import info.peigen.hotpot.common.core.result.ListResult;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.SingleResult;
import info.peigen.hotpot.common.service.base.event.LogEventHandler;
import info.peigen.hotpot.common.service.base.event.ServiceResultLogEvent;
import info.peigen.hotpot.component.open.api.facade.processor.base.OpenApiProcessor;
import info.peigen.hotpot.component.open.api.facade.processor.base.OpenApiProcessorServiceContext;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(OpenApiPostLogEventProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/6/13
 */
@SuppressWarnings("rawtypes")
@Component
public class OpenApiPostLogEventProcessor implements OpenApiProcessor {

    @Inject
    protected LogEventHandler logEventHandler;

    @Override
    public Integer level() {
        return BOTTOM_LEVEL;
    }

    @Override
    public void execute(OpenApiProcessorServiceContext serviceContext) {
        // 6. 打印[结果]日志
        ServiceResultLogEvent resultLogEvent = ServiceResultLogEvent.builder()
                .productCode(serviceContext.getOrder().getProductCode())
                .bizOrderNo(serviceContext.getOrder().getBizOrderNo())
                .build();

        if (serviceContext.getResult() instanceof PageResult) {
            resultLogEvent.setPageResult((PageResult<?>) serviceContext.getResult());
        }

        if (serviceContext.getResult() instanceof ListResult) {
            resultLogEvent.setListResult((ListResult<?>) serviceContext.getResult());
        }

        if (serviceContext.getResult() instanceof SingleResult) {
            resultLogEvent.setSingleResult((SingleResult<?>) serviceContext.getResult());
        }

        logEventHandler.eventPost(resultLogEvent);
    }

}